package cn.nkjobsearch.convert;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Salary {
	public static int convert(String salary, char fromSite) {
		if (fromSite == '5') {
			return job51(salary);
		} else if (fromSite == 'c') {
			return chinaHR(salary);
		} else if (fromSite == 'z') {
			return zhilian(salary);
		} else {
			return 0;
		}
	}

	/**
	 * 从数据库中的 薪水编码 获得 薪水描述
	 * 
	 * @param code
	 * @return
	 */
	public static String getSalaryViaCode(String code) {
		if ("0".equals(code))
			return "面议";
		else
			return code;
	}

	/**
	 * 如果公司名称不存在 相应处理
	 * 
	 * @param code
	 * @return
	 */
	public static String getOrgNameViaCode(String code) {
		if (null == code || "".equals(code))
			return "不详";
		else
			return code;
	}

	/**
	 * 从数据库中的 三个网站编码 获得对应的 网站名称
	 * 
	 * @param code
	 * @return
	 */
	public static String getSiteViaCode(String code) {
		if ("c".equals(code))
			return "中华英才网";
		else if ("z".equals(code))
			return "智联招聘";
		if ("5".equals(code))
			return "前程无忧";
		else
			return "其他";
	}

	/**
	 * 从数据库中的 site jobID jobDate 获得job信息的 完整URL地址
	 * @param site
	 * @param jobID
	 * @param jobDate
	 * @return
	 */
	public static String getJobUrlViaCode(String site, String jobID,
			String jobDate) {
		if ("c".equals(site)) {
			return "http://jobs.chinahr.com/html/" + jobID.substring(20, 27)
					+ "/" + jobID.substring(28, 30) + "/"
					+ jobID.substring(0, 20) + ".htm";
		} else if ("z".equals(site)) {
			return "http://jobs.zhaopin.com/P" + jobID.substring(21) + "/CC"
					+ jobID.substring(0, 4) + "/" + jobID.substring(4, 8) + "/"
					+ jobID.substring(9, 13) + "/" + jobID.substring(13, 16)
					+ "/CC" + jobID.substring(0, 21) + ".htm";
		}
		if ("5".equals(site)) {
			return "http://search.51job.com/jobsearch/show_job_detail.php?id=("
					+ jobID + ")";
		} else {
			return "404.html";
		}
	}

	private static int job51(String salary) {
		if (salary == null || salary.equals("null") || salary.equals("面议")) {
			return 0;
		}
		try {
			int separationIndex = salary.indexOf((int) '-');
			if (separationIndex > 3) {
				return Integer.parseInt(salary.substring(0, separationIndex));
			} else {
				Pattern p = Pattern.compile("[0-9]{4,6}",
						Pattern.CASE_INSENSITIVE);
				Matcher m = p.matcher(salary);
				if (m.find()) {
					return Integer.parseInt(salary
							.substring(m.start(), m.end()));
				}
			}
		} catch (Exception e) {
			return 0;
		}
		return 0;
	}

	private static int zhilian(String salary) {
		if (salary == null || salary.equals("null") || salary.equals("面议")) {
			return 0;
		}
		try {
			int separationIndex = salary.indexOf((int) '-');
			if (separationIndex > 3) {
				return Integer.parseInt(salary.substring(0, separationIndex));
			} else {
				Pattern p = Pattern.compile("[0-9]{4,6}",
						Pattern.CASE_INSENSITIVE);
				Matcher m = p.matcher(salary);
				if (m.find()) {
					return Integer.parseInt(salary
							.substring(m.start(), m.end()));
				}
			}
		} catch (Exception e) {
			return 0;
		}
		return 0;
	}

	private static int chinaHR(String salary) {
		if (salary == null || salary.equals("null") || salary.equals("面议")) {
			return 0;
		}
		try {
			int separationIndex = salary.indexOf("～");
			if (separationIndex > 3) {
				return Integer.parseInt(salary.substring(0, separationIndex));
			} else {
				Pattern p = Pattern.compile("[0-9]{4,6}",
						Pattern.CASE_INSENSITIVE);
				Matcher m = p.matcher(salary);
				if (m.find()) {
					return Integer.parseInt(salary
							.substring(m.start(), m.end()));
				}
			}
		} catch (Exception e) {
			return 0;
		}
		return 0;
	}
}
